// pages/newItem/newItem.ts
import { Request ,handleFileInUploading} from '../../utils/util'
import Notify from '@vant/weapp/notify/notify';
const app = getApp()
Page({
    data: {
        startDay: "",
        endDay: "",
        startTime: '00:00',
        endTime: '23:59',
        activitycode:"",
        description:"",
        note:"",
        cover:NaN,
        coverUrl:"",
        description:"",
        responseuser:"",
        showEndCalendar: false,
        showStartCalendar: false,
        showStartTimePicker: false,
        showEndTimePicker: false,
        currentTime: '',
        loading:true
    },
    onLoad() {
        let date = new Date()
        let curDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
        let hour=date.getHours()>10?date.getHours():`0${date.getHours()}`
        let minute=date.getMinutes() + 1>10?date.getMinutes() + 1:`0${date.getMinutes()}`
        console.log("minute",hour,minute)
        this.setData({
            startDay: curDate,
            endDay: curDate,
            currentTime: `${hour}:${minute}`,
            startTime: `${hour}:${minute}`,
            endTime:"23:59"
        })
        console.log(this.data.startDay,this.data.startTime)
    },
    onReady(){
        this.setData({loading:false})
    },
    chooseDateEnd() {
        console.log("choseDare")
        this.setData({
            showEndCalendar: true,
        })
    },
    onCloseEnd() {
        this.setData({ showEndCalendar: false });
    },
    formatDate(date) {
        date = new Date(date);
        return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
    },
    onConfirmEnd(event) {
        this.setData({
            showEndCalendar: false,
            endDay: this.formatDate(event.detail),
        });
    },

    chooseDatStart() {
        this.setData({
            showStartCalendar: true,
        })
    },
    onCloseStart() {
        this.setData({ showStartCalendar: false });
    },
    onConfirmStart(event) {
        this.setData({
            showStartCalendar: false,
            startDay: this.formatDate(event.detail),
        });
    },
    chooseTimeStart() {
        this.setData({ showStartTimePicker: true })
    },

    chooseTimeEnd() {
        this.setData({ showEndTimePicker: true })
    },
    confirmStartTime(e) {
        this.setData({ startTime: e.detail })
    },
    cancelStartTime(e) {
        this.setData({ showEndTimePicker: false })
    },
    onClickHideStart() {
        this.setData({ showStartTimePicker: false })
    },
    confirmEndTime(e) {
        if(e.detail=='aN:aN'){
            e.detail='00:00'
        }
        this.setData({ endTime: e.detail })
    },
    cancelEndTime(e) {
        this.setData({ showEndTimePicker: false })
    },

    onClickHideEnd() {
        this.setData({ showEndTimePicker: false })
    },
    submitItem(){
        let _this=this
        if(this.data.activitycode==""){
            Notify({
                color: '#ffffff',
                background: app.globalData.notifyColor,
                message: '记得填项目名！',
                top: app.globalData.navBarHeight
            });
        }else if(this.data.responseuser==""){
            Notify({
                color: '#ffffff',
                background: app.globalData.notifyColor,
                message: '记得填项目负责人！',
                top: app.globalData.navBarHeight
            });
        }else if(this.data.coverUrl==""){
            Notify({
                color: '#ffffff',
                background: app.globalData.notifyColor,
                message: '记得上传海报！',
                top: app.globalData.navBarHeight
            });
        }else{
            wx.showModal({
                title: '提示',
                content: '确定要创建项目吗？',
                success(res) {
                    if (res.confirm) {
                        Request({
                            url: '/newborrowteam',
                            method:'post',
                            data:{
                                data:{
                                    "activitycode":_this.data.activitycode,
                                    "responseuser":_this.data.responseuser,
                                    "description":_this.data.description,
                                    "coverUrl":_this.data.coverUrl,
                                    "borrowtime":new Date(_this.data.startDay+' '+_this.data.startTime),
                                    "expectReturnTime":new Date(_this.data.endDay+' '+_this.data.endTime),
                                    "note":_this.data.note
                                }
                            }
                        }).then(res => {
                            if(res.success==true){
                                Notify({
                                    color: '#ffffff',
                                    background: '#F7608B',
                                    message: '创建成功',
                                    top: app.globalData.navBarHeight
                                });
                                setTimeout(function () {
                                    wx.reLaunch({
                                        url: '/pages/itemGoing/itemGoing'
                                    });
                                }, 2000)
                            }else{
                                Notify({
                                    color: '#ffffff',
                                    background: '#F7608B',
                                    message: '创建失败'
                                });
                            }
                        }).catch(error => {
                            console.log(error);
                        });
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }
    },
    chooseCover(){
          //上传图片
          console.log("choose cover")
        var that = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                console.log(res)
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                that.setData({
                    cover: res.tempFilePaths[0],
                })
                let timestamp = new Date().getTime();
                let imgType=res.tempFilePaths[0].split('.')[1]
                let imgName=`itemCover/${timestamp}.${imgType}`
                console.log("type",res.tempFilePaths[0].split('.'))
                handleFileInUploading(imgName,res.tempFilePaths[0])
                console.log(res.tempFilePaths[0])
                let url=`https://seubilibili-1300357628.cos.ap-nanjing.myqcloud.com/${imgName}`
                that.setData({
                    coverUrl: url,
                })
                console.log(url)
            }
        })
    
    }
})